<template>
  <view>
    <button style="margin-top: 50px" open-type="share" @click="share">
      分享
    </button>

    <button style="margin-top: 50px" @click="getLocation">获取经纬度</button>

    <button style="margin-top: 50px" @click="fnPay">支付</button>

    <ul>
      <li
        v-for="(item, index) in movies"
        :key="index"
        @click="changeBgRed(index)"
        :class="{ bg: index == activeIndex }"
      >
        {{ item }}
      </li>
    </ul>
  </view>
</template>

<script>
import { request, requestData } from '@/utils/http.js'
export default {
  data() {
    return {
      movies: ['11', '22', '333', '44'],
      activeIndex: 0,
    }
  },
  onShow: function () {
    //获取在app.vue中定义的全局数据
    this.tips = getApp().globalData.text
    console.log(getApp().globalData.text)

    this.templateList()
  },
  methods: {
    changeBgRed(index) {
      this.activeIndex = index
    },
    //允许转发的默认函数
    onShareAppMessage() {},

    // 分享操作
    share() {
      uni.share({
        provider: 'weixin',
        scene: 'WXSceneSession',
        type: 1,
        summary: '我正在使用HBuilderX开发uni-app，赶紧跟我一起来体验！',
        success: function (res) {
          console.log('success:' + JSON.stringify(res))
        },
        fail: function (err) {
          console.log('fail:' + JSON.stringify(err))
        },
      })
    },

    //经纬度
    getLocation() {
      uni.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res)
        },
        fail: function (error) {
          console.error('获取位置失败：', error)
        },
      })
    },

    templateList() {
      request('/common/ticket/template/list', {
        user_id: uni.getStorageSync('user_id'),
        site_code: '17001',
      })
        .then((res) => {
          console.log(res)
          // healthPic.value = res?.datas?.url
          // uni.showToast({
          // 	title: '上传成功！',
          // 	icon: 'success',
          // })
        })
        .catch((err) => {
          console.log(err)
        })
    },

    //支付
    //https://blog.csdn.net/m0_70027114/article/details/139039361
    fnPay() {
      request('/user/ticket/buy/wx', {
        user_id: uni.getStorageSync('user_id'),
        template_id: '2011174064492721665',
      })
        .then((res) => {
          console.log(res)
          uni.removeStorageSync('appId')
          uni.removeStorageSync('timeStamp')
          uni.removeStorageSync('nonceStr')
          uni.removeStorageSync('package')
          uni.removeStorageSync('signType')
          uni.removeStorageSync('paySign')
          uni.setStorageSync('appId', res?.datas?.appId || '')
          uni.setStorageSync('timeStamp', res?.datas?.timeStamp || '')
          uni.setStorageSync('nonceStr', res?.datas?.nonceStr || '')
          uni.setStorageSync('package', res?.datas?.package || '')
          uni.setStorageSync('signType', res?.datas?.signType || '')
          uni.setStorageSync('paySign', res?.datas?.paySign || '')

          this.fnWxPay()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    fnWxPay() {
      uni.requestPayment({
        timeStamp: uni.getStorageSync('timeStamp'), //时间戳
        nonceStr: uni.getStorageSync('nonceStr'), //随机字符串
        package: uni.getStorageSync('package'), //prepay_id
        signType: uni.getStorageSync('signType'), //签名算法MD5
        paySign: uni.getStorageSync('paySign'), //签名
        success(res) {
          console.log(res)
          if (res.errMsg == 'requestPayment:ok') {
            uni.showToast({
              title: '支付成功！',
              icon: 'success',
            })
          } else {
            uni.showToast({
              title: '支付取消！',
              icon: 'success',
            })
          }
        },
        fail(res) {
          uni.showToast({
            title: '支付失败',
            icon: 'success',
          })
        },
      })
    },
  },
}
</script>

<style>
li {
  list-style: none;
  cursor: pointer;
  width: 300px;
}

.bg {
  background-color: red;
  color: #fff;
}
</style>
